// 轮播图
function Carousel() {
    this.goLeft = $('.icon-left-line');
    this.goRight = $('.icon-right-line');
    this.carouselContainer = $('#carousel-container');
    this.carouselImage = $('#carousel-img');
    this.index = 0;

    var that = this;

    this.timer = setInterval(function() {
        that.toRight();
    }, 3000);

    this.imgArr = [
        `0.png`,
        `1.webp`,
        `2.webp`
    ];

    this.doit = function() {
        this.goLeft.stop().on('click', function() {
            that.toLeft();
        });

        this.goRight.stop().on('click', function() {
            that.toRight();
        });
    }

    this.changeImage = function() {
        clearInterval(this.timer);

        this.carouselContainer.css({
            'background-image': `url(../images/${this.imgArr[this.index]})`
        });
        this.carouselImage.css({
            'background-image': `url(../images/${this.imgArr[this.index]})`
        });

        this.timer = setInterval(function() {
            that.toRight();
        }, 3000);
    }

    this.toLeft = function() {
        if (this.index == 0) {
            this.index = this.imgArr.length - 1;
        } else {
            this.index--;
        }
        this.changeImage();
    }

    this.toRight = function() {
        if (this.index == this.imgArr.length - 1) {
            this.index = 0;
        } else {
            this.index++;
        }
        this.changeImage();
    }
}


// 兴趣导航
function interest() {
    $('.nav-item').on('click', function() {
        $(this).addClass('active-nav-item').stop().siblings().removeClass('active-nav-item');
    });
}

// 免费直播课
function freeLesson() {
    $('.course-list>.list-item').on('mouseenter', function() {
        $(this).addClass('active-item').stop().siblings().removeClass('active-item');
    })
}

$(function() {

    // 轮播图
    var carousel = new Carousel();
    carousel.doit();

    // 兴趣导航
    interest();

    // 免费直播课
    freeLesson();
});